﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RTSoft.Modules;
using System.Data.SqlClient;
using System.Data;

namespace RTSoft.SoureCode.Control.HoaDon
{
    class hoadonctrl
    {
        DataProvider provider = new DataProvider();
        SqlCommand cmd = new SqlCommand();
        SqlDataReader reader;
        String strSQL = "";

        public int getID(ref String errConnect, ref String errSQL)
        {
            int id = 0;
            try
            {
                cmd.Connection = provider.Connection(ref errConnect);
                strSQL = "select Max(ID_HD) from HoaDon ";
                cmd.CommandText = strSQL;
                reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    id = Convert.ToInt32(reader[0]);
                }
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
            }
            finally
            {
                provider.Disconnect();
            }
            return id;
        }

        public void getData(ref String errConnect, ref String errSQL, ref DataTable tbl)
        {
            provider.Connection(ref errConnect);
            try
            {
                tbl = new DataTable();
                strSQL = "	select ID_HD,ID_KH,ID_NV,THANHTIEN,NGAY_TAO,CASE TRANGTHAI WHEN 1 THEN 'Đã thanh toán' WHEN 0 THEN 'Chưa thanh toán' END as TRANGTHAI from HOADON WHERE TRANGTHAI = 0";
                tbl = provider.LayDanhSach(strSQL);
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
            }
            finally
            {
                provider.Disconnect();
            }
        }

        public void Insert(ref String errConnect, ref String errSQL, hoadoninfo info)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                SqlParameter f = new SqlParameter();
                f.IsNullable = true;
                f.Value = System.DBNull.Value;
                strSQL = "Insert into HOADON";
                strSQL = strSQL + "(ID_NV,ID_KH,THANHTIEN,NGAY_TAO,TRANGTHAI)";
                strSQL = strSQL + " values";
                strSQL = strSQL + "(@ID_NV,@ID_KH,@THANHTIEN,@NGAY_TAO,@TRANGTHAI)";
                cmd.CommandText = strSQL;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ID_NV", info.ID_NV);
                cmd.Parameters.AddWithValue("@ID_KH", info.ID_KH);
                cmd.Parameters.AddWithValue("@THANHTIEN", info.THANHTIEN);
                cmd.Parameters.AddWithValue("@NGAY_TAO", info.NGAY_TAO);
                cmd.Parameters.AddWithValue("@TRANGTHAI", info.TRANGTHAI);
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }

        }

        public void Update(ref String errConnect, ref String errSQL, hoadoninfo info)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                SqlParameter f = new SqlParameter();
                f.IsNullable = true;
                f.Value = System.DBNull.Value;
                strSQL = "Update HOADON SET  ";
                strSQL = strSQL + "ID_NV=@ID_NV,ID_KH=@ID_KH,THANHTIEN=@THANHTIEN,NGAY_TAO=@NGAY_TAO,TRANGTHAI=@TRANGTHAI Where  ID_HD = @ID_HD";
                cmd.CommandText = strSQL;

                cmd.Parameters.AddWithValue("@ID_HD", info.ID_HD);
                cmd.Parameters.AddWithValue("@ID_NV", info.ID_NV);
                cmd.Parameters.AddWithValue("@ID_KH", info.ID_KH);
                cmd.Parameters.AddWithValue("@THANHTIEN", info.THANHTIEN);
                cmd.Parameters.AddWithValue("@NGAY_TAO", info.NGAY_TAO);
                cmd.Parameters.AddWithValue("@TRANGTHAI", info.TRANGTHAI);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }

        }

        public void Delete(ref String errConnect, ref String errSQL, int id)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                strSQL = "DELETE FROM HOADON WHERE ID_HD=@RowID";
                cmd.Parameters.AddWithValue("@RowID", id);
                cmd.CommandText = strSQL;
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
                //' Thuc hien transaction 
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }
        }
    }
}
